<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>无标题文档</title>
  <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css" media="all" />
  <link rel="stylesheet" href="${pageContext.request.contextPath}/css/style.css" type="text/css" />
  <script src="${pageContext.request.contextPath}/layui/jquery2.1.1.js"></script>
  <script src="https://www.layuicdn.com/layui-v2.5.6/layui.js"></script>
  <style type="text/css">
    .layui-form-item i{
      display: block;
      margin-top: 10px;
    }
  </style>
</head>

<body>
<div class="place"> <span>位置：</span>
  <ul class="placeul">
    <li>采购管理</li>
    <li>厂商管理</li>
    <li>修改</li>
  </ul>
</div>
<form class="layui-form" action="">
<div class="formbody">
  <input name="id" id="id" type="text" class="layui-input" style="display: none"/>
  <div class="formtitle"><span>厂商信息</span></div>
  <div class="layui-form-item">
    <label class="layui-form-label">厂商名称</label>
    <div class="layui-input-inline" >
      <input name="name" id="name" type="text" value="" class="layui-input" lay-verify="required"/>
    </div>
    <div class="layui-input-inline" >
      <i>必填，不能超过50个字符</i>
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">负责人</label>
    <div class="layui-input-inline" >
      <input name="userPerson" id="userPerson" type="text" value="" class="layui-input" lay-verify="required" />
    </div>
    <div class="layui-input-inline" >
      <i>必填，不能超过30个字符</i>
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">联系方式</label>
    <div class="layui-input-inline" >
      <input name="tel" id="tel" type="text" value="" class="layui-input" lay-verify="required"/>
    </div>
    <div class="layui-input-inline" >
      <i>必填，不能超过30个字符</i>
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">联系地址</label>
    <div class="layui-input-inline" >
      <input name="address" id="address" type="text" value="" class="layui-input" lay-verify="required"/>
    </div>
    <div class="layui-input-inline" >
      <i>必填，不能超过130个字符</i>
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">所属区域</label>
    <div class="layui-input-inline" >
      <select name="province" id="province" lay-filter="province" lay-verify="required">
        <option value="0">请选择省份</option>
      </select>
    </div>
    <div class="layui-input-inline" >
      <select name="city" id="city" lay-filter="city" lay-verify="required">
        <option value="0">请选择城市</option>
      </select>
    </div>
    <div class="layui-input-inline" >
      <i>必选</i>
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">描述</label>
    <div class="layui-input-inline" >
      <textarea name="description" id="description" type="text" value="" class="layui-input" ></textarea>
    </div>
    <div class="layui-input-inline" >
      <i>可选</i>
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">状态</label>
    <div class="layui-input-inline" >
      <select name="status" id="status" lay-verify="required">
        <option value="0">请选择</option>
        <option value="1">可用</option>
        <option value="2">不可用</option>
      </select>
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">创建人</label>
    <div class="layui-input-inline" >
      <input name="createPerson" id="createPerson" type="text" value="" readonly="readonly" class="layui-input" />
    </div>
    <div class="layui-input-inline" >
      <i>不能编辑</i>
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">创建时间</label>
    <div class="layui-input-inline" >
      <input name="createTime" id="createTime" type="text" value="" readonly="readonly" class="layui-input" />
    </div>
    <div class="layui-input-inline" >
      <i>不能编辑</i>
    </div>
  </div>
  <div class="layui-form-item">
    <button lay-submit lay-filter="formDemo" class="layui-btn btn-add">修改</button>
  </div>
</div>
</form>
<script type="text/javascript">
  layui.use(['table', 'form'], function () {
    var table = layui.table,
            form = layui.form,
            util = layui.util,
            $ = layui.jquery;
    form.render();
    $.ajax({
      url: "/region/provice",
      type: "post",
      dataType: "json",
      success: function(data) {
        var json = eval("("+data+")");

        //使用循环遍历，给下拉列表赋值
        $("#province").empty();
        $("#province").append(new Option("请选择省份",""))
        for(var i=0;i<json.length;i++){
          $("#province").append('<option value="'+json[i].name+'">'+json[i].name+'</option>');
        }
        form.render("select")//重新渲染 固定写法
      }
    });
//省份城市联动
    form.on('select(province)',function (data) {
      var pname=data.value;
      if(pname==''){
        $('#city').empty();
        $('#city').append('<option value="">请选择城市</option>');
      }else{
        $('#city').empty();
        $('#city').append('<option value="">请选择城市</option>');
        $.ajax({
          url: '/region/city?pname='+pname,
          type: 'post',
          data: 'pname='+pname,
          //dataType: 'json',
          //contentType:"application/json",
          success: function (resps) {
            layui.$('#city').empty();
            layui.$('#city').append('<option value="">请选择城市</option>');
            var json=JSON.parse(resps);
            var jsonList=eval("("+json+")");
            for(var i=0;i<jsonList.length;i++) {
              var name=jsonList[i].cName;
              layui.$('#city').append(
                      '<option value="'+name+'">'+name+'</option>'
              );
            }
            layui.form.render();
          },
          error: function () {
            layer.closeAll('loading');
            layer.msg('系统错误，请联系管理员', {icon: 5});
          }
        });
      }
      form.render();
    });
    $(function () {
      var parent_json = eval('('+parent.json+')');
      $("#id").val(parent_json.id);
      $.ajax({
        url: "/Firm/findFirmById?id="+$("#id").val(),
        type: 'post',
        dataType: 'json',
        success: function (res) {
          var data = res;
          $("#name").val(data.name);
          $("#userPerson").val(data.userPerson);
          $("#tel").val(data.tel);
          $("#name").val(data.name);
          $("#province").val(data.province);
          $("#status").val(data.status);
          $("#city").val(data.city);
          $("#address").val(data.address);
          $("#description").val(data.description);
          $("#createPerson").val(data.createPerson);
          $("#createTime").val(util.toDateString(data.createTime, "yyyy-MM-dd HH:mm:ss"));
        },
        error: function () {
          layer.msg('系统错误，请联系管理员', {icon: 5});
        }
      });
      form.render();
    })
    //监听提交
    form.on('submit(formDemo)', function (data) {
      $.ajax({
        url: "/Firm/updateFirm",
        data: JSON.stringify(data.field),
        type: 'post',
        dataType: 'json',
        contentType: "application/json",
        success: function (resp) {
          layer.closeAll();
          layer.msg(resp.message, {icon: 6});
        },
        error: function () {
          layer.closeAll();
          layer.msg('系统错误，请联系管理员', {icon: 5});
        }
      });
      $(function () {
        layer.closeAll();
        table.reload('PurchaseTable', {
          where: {},
          data:[]
        });
      });
      return false;
    });
  });
</script>
</body>
</html>
